﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Security.Cryptography;


namespace Stc.Dbo
{
    /// <summary>
    /// DES 的摘要说明
    /// </summary>
    public class Secrecy
    {
        private byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };//默认密钥向量

        public Secrecy()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //

        }
        /// <summary>
        /// 对数字进行加密
        /// </summary>
        /// <param name="encryptString">预加密字符串</param>
        /// <param name="decryptKey">加密常量</param>
        /// <returns>string</returns>
        public string EncryptNumber(string encryptString, string decryptKey)
        {
            if (encryptString.Equals("") || decryptKey.Equals(""))
            {
                return "Can not be null.";
            }
            else if (!StaticUtil.RegularCheck(encryptString,"^[0-9]*$"))
            {
                return "Please input number.";
            }

            string strResult = "";
            foreach (char c in encryptString)
            {
                int iTmp;
                iTmp = Convert.ToInt32(c);
                iTmp += ASII(decryptKey);

                char cTmp = Convert.ToChar(iTmp);

                strResult += cTmp.ToString();
            }
            return strResult;
        }
        /// <summary>
        /// 对数字进行解密
        /// </summary>
        /// <param name="encryptString">预解密字符串</param>
        /// <param name="decryptKey">加密常量</param>
        /// <returns>string</returns>
        public string DecryptNumber(string encryptString, string decryptKey)
        {
            if (encryptString.Equals("") || decryptKey.Equals(""))
            {
                return "Can not be null.";
            }
            else if (!StaticUtil.RegularCheck(encryptString, "^[0-9]*$"))
            {
                return "Please input number.";
            }

            string strResult = "";
            foreach (char c in encryptString)
            {
                int iTmp;
                iTmp = Convert.ToInt32(c);
                iTmp -= ASII(decryptKey);

                char cTmp = Convert.ToChar(iTmp);

                strResult += cTmp.ToString();
            }
            return strResult;
        }

        protected static int ASII(string s)
        {
            int i = 0;
            string str = "";
            foreach (char c in s)
            {
                str += Convert.ToInt32(c).ToString();
                i = Convert.ToInt32(str);
                while (i < 17 || i > 33)
                {
                    if (i > 33)
                    {
                        i -= 33;
                    }
                    else if (i < 17)
                    {
                        i += 17;
                    }
                }
                str = i.ToString();
            }
            return i;
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="PassWord">预加密的密码</param>
        /// <returns>密文</returns>
        public string MD5(string PassWord)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(PassWord, "md5");
        }

        /// <summary>
        /// Des 加密
        /// </summary>
        /// <param name="encryptString">预解密字符串</param>
        /// <param name="encryptKey">加密常量</param>
        /// <returns>密文</returns>
        public string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return "Attention,Encode Failure!";
            }
        }

        /// <summary>
        /// Des 解密
        /// </summary>
        /// <param name="decryptString">预解密字符串</param>
        /// <param name="decryptKey">加密常量</param>
        /// <returns>密文</returns>
        public string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return "Attention,Decode Failure!";
            }
        }


    }
}